package com.yr.interceptor;

import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 这是设置拦截器做什么 还需要去配置拦截器拦截哪些资源
 */
public class LoginInterceptor extends HandlerInterceptorAdapter {

    @Override
    public boolean preHandle(HttpServletRequest request,
                             HttpServletResponse response,
                             Object handler) throws Exception {
        //如果未登录
        if (request.getSession().getAttribute("user") == null) {
            response.sendRedirect("/admin");
            //默认false，拦截后面的资源无法访问
            return false;
        }
        //登录成功了session域中有数据，放行后面资源
        return true;
    }
}
